<script>
import {getAddLabel, getDelLabel, getEditLabel, getLabel} from "@/utils/class/Pluponform";

export default {
  name: "labelPage",
  data() {
    return {
      currentType: '0',
      removeList: [],
      //添加
      dialogVisible: false,
      //表格
      tableData: [],
      form: {
        name: ''
      },
      //渲染传参
      queryList: {
        pageSize: '',
        pageNum: ''
      },
      //新增传参
      addQuery: {
        label: ''
      },
      //编辑传参
      editQuery: {
        id: '',
        label: ''
      }

    };
  },
  methods: {
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => {
        });
      this.dialogVisible = true
    },
    add() {
      this.dialogVisible = true
      this.form.name = ''
    },
    //点击编辑
    handleClick(row) {
      // console.log(row);
      this.currentType = 1
      this.dialogVisible = true
      this.editQuery.id = row.id
    },

    //添加、编辑完保存
    sure() {
      if (this.currentType === '0') {
        if (this.form.name === '') {
          this.$message.warning('标签不能为空')
        } else {
          this.dialogVisible = false
          this.addQuery.label = this.form.name
          getAddLabel(this.addQuery).then(res => {
            //  console.log(res)
            this.$message.success("添加成功")
            this.getLabelList()
          })
        }

      } else {
        this.dialogVisible = false
        this.editQuery.label = this.form.name
        getEditLabel(this.editQuery).then(res => {
          console.log(res)
          this.$message.success("编辑成功")
          this.getLabelList()
        })
      }

    },

    //删除
    deletePage(item) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        getDelLabel(item.id).then(res => {
          // console.log(res)
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.getLabelList()
        })

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
//渲染
    getLabelList() {
      getLabel(this.queryList).then(res => {
        //console.log(res)
        this.tableData = res.rows
      })
    }
  },
  created() {
    this.getLabelList()
  }
}
</script>

<template>
  <div>
    <div style="padding: 40px">
      <!--      添加-->
      <div class="top">
        <el-button type="primary" @click="add">添加</el-button>
        <el-dialog
          :before-close="handleClose"
          :title="currentType === '0' ? '添加' : '编辑'"
          :visible.sync="dialogVisible"
          width="40%">
          <el-form ref="form" :model="form" label-width="80px">
            <el-form-item label="标签名称">
              <el-input v-model="form.name"></el-input>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="sure">保 存</el-button>
  </span>
        </el-dialog>
      </div>
      <!--      表格-->
      <div class="main" style="margin-top: 30px">
        <el-table
          :data="tableData"
          border
          style="width: 100%">
          <el-table-column
            label="名称"
            prop="label">
          </el-table-column>
          <el-table-column
            label="更新时间"
            prop="updateTime"
            width="500"
          >
          </el-table-column>
          <el-table-column
            fixed="right"
            label="操作"
            prop="operate"
            width="500">
            <template slot-scope="scope">
              <el-button size="small" type="text" @click="handleClick(scope.row)">编辑</el-button>
              <el-button size="small" type="text" @click="deletePage(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
::v-deep.el-table th.el-table__cell.is-leaf, .el-table td.el-table__cell {
  text-align: center;
}

::v-deep.el-table td.el-table__cell div {
  text-align: center;
}
</style>

